<template>
	<view class="content">
		<!-- <uni-popup ref="popup" @change="change">
			<view class="popup-content">
				<view class="warp">
					<button class="button" type="primary" @click="setFormType(1)">吊装令</button>
					<button class="button" type="primary" @click="setFormType(2)">动火令</button>
					<button class="button" type="primary" @click="setFormType(3)">动土令</button>
				</view>
			</view>
		</uni-popup> -->
		<view class="content-form">
			<uni-forms ref="baseForm" :rules="rules" :modelValue="formData" label-position="top">
				<uni-forms-item label="施工部位" required name="constructionSite">
					<uni-easyinput v-model="formData.constructionSite" placeholder="请输入施工部位" />
				</uni-forms-item>
				<uni-forms-item label="作业班组" required name="workTeam">
					<!-- <uni-data-picker placeholder="请选择作业班组" popup-title="请选择作业班组" :localdata="dataTree"
						v-model="formData.workTeam">
					</uni-data-picker> -->
					<uni-easyinput v-model="formData.workTeam" placeholder="请输入作业班组" />
				</uni-forms-item>
				<uni-forms-item label="申请事由" required name="reasonForApplication">
					<uni-easyinput type="textarea" v-model="formData.reasonForApplication" placeholder="请输入申请事由" />
				</uni-forms-item>
				<uni-forms-item label="开始时间" required name="startTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.startTime"
						placeholder="请选择开始时间" />
				</uni-forms-item>
				<uni-forms-item label="结束时间" required name="endTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.endTime"
						placeholder="请选择结束时间" />
				</uni-forms-item>
				<view v-if="formData.type==1">
					<uni-forms-item label="起重设备" required name="liftingEquipment">
						<!-- 	<uni-data-picker placeholder="请选择起重设备" popup-title="请选择起重设备" :localdata="dataTree"
							v-model="formData.liftingEquipment">
						</uni-data-picker> -->
						<uni-easyinput v-model="formData.liftingEquipment" placeholder="请输入起重设备" />
					</uni-forms-item>
					<uni-forms-item label="设备编号" required name="equipmentCode">
						<uni-easyinput v-model="formData.equipmentCode" placeholder="请输入设备编号" />
					</uni-forms-item>
					<uni-forms-item label="吊装级别" required name="level">
						<uni-data-picker placeholder="请选择吊装级别" popup-title="请选择吊装级别" :localdata="levelList"
							v-model="formData.level">
						</uni-data-picker>
					</uni-forms-item>
					<uni-forms-item label="吊装人员" required name="operatives">
						<uni-easyinput v-model="formData.operatives" placeholder="请输入吊装人员" />
					</uni-forms-item>
					<uni-forms-item label="作业证件" required name="homeworkCertificate">
						<uni-file-picker v-model="imageValue" limit="3" title="最多选择3张图片" @select="select"
							@delete="delIMG"></uni-file-picker>
					</uni-forms-item>
					<uni-forms-item label="吊装指挥" required name="commandId">
						<uni-data-picker placeholder="请选择吊装指挥人员" popup-title="请选择吊装指挥人员" :localdata="userList"
							v-model="formData.commandId" @change="e=>getSelectUserName(e,'commandName')">
						</uni-data-picker>
					</uni-forms-item>


				</view>
				<view v-if="formData.type==2">

					<uni-forms-item label="动火类型" required name="hotWorkType">
						<uni-data-picker placeholder="请选择动火类型" popup-title="请选择动火类型" :localdata="hotWorkList"
							v-model="formData.hotWorkType">
						</uni-data-picker>
					</uni-forms-item>

					<uni-forms-item label="动火等级" required name="level">
						<uni-data-picker placeholder="请选择动火等级" popup-title="请选择" :localdata="levelList"
							v-model="formData.level">
						</uni-data-picker>
					</uni-forms-item>
					<uni-forms-item label="作业人员" required name="operatives">
						<uni-easyinput v-model="formData.operatives" placeholder="请输入作业人员" />
					</uni-forms-item>

					<uni-forms-item label="作业证件" required name="homeworkCertificate">
						<uni-file-picker v-model="imageValue" limit="3" title="最多选择3张图片" @select="select"
							@delete="delIMG"></uni-file-picker>
					</uni-forms-item>


				</view>
				<view v-if="formData.type==3">
					<uni-forms-item label="开挖深度" required name="depth">
						<uni-easyinput v-model="formData.depth" placeholder="请输入开挖深度" />(米)
					</uni-forms-item>
					<uni-forms-item label="支护方式" required name="supportMethod">
						<uni-easyinput v-model="formData.supportMethod" placeholder="请输入支护方式" />
					</uni-forms-item>
					<uni-forms-item label="作业人员" required name="operatives">
						<uni-easyinput v-model="formData.operatives" placeholder="请输入作业人员" />
					</uni-forms-item>
					<uni-forms-item label="作业人数" required name="peopleCount">
						<uni-easyinput v-model="formData.peopleCount" placeholder="请输入作业人数" />
					</uni-forms-item>
					<!-- <uni-forms-item label="作业证件" required name="homeworkCertificate">
						<uni-file-picker v-model="imageValue" limit="3" title="最多选择3张图片" @select="select"
							@delete="delIMG"></uni-file-picker>
					</uni-forms-item> -->
				</view>
				<uni-forms-item label="现场监护人" required name="guardianId">
					<uni-data-picker placeholder="请选择监护人员" popup-title="请选择监护人员" :localdata="userList"
						v-model="formData.guardianId" @change="e=>getSelectUserName(e,'guardianName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="班组负责人" required name="applicationManagerId">
					<uni-data-picker placeholder="请选择班组负责人" popup-title="请选择班组负责人" :localdata="userList"
						v-model="formData.applicationManagerId"
						@change="e=>getSelectUserName(e,'applicationManagerName')">
					</uni-data-picker>
				</uni-forms-item>

			</uni-forms>
			<button type="primary" @click="submit('baseForm')">提交</button>

		</view>

	</view>
</template>

<script>
	import {
		listUser
	} from "@/api/system/user.js"
	import {
		addHomework
	} from "@/api/homework";
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				show: false,
				formData: {
					id: null,
					type: 3,
					applicationTime: null,
					constructionSite: null,
					workTeam: null,
					liftingEquipment: null,
					equipmentCode: null,
					reasonForApplication: null,
					hotWorkType: null,
					level: null,
					startTime: null,
					endTime: null,
					operatives: null,
					depth: null,
					supportMethod: null,
					peopleCount: null,
					homeworkCertificate: [],
					commandId: null,
					commandName: null,
					guardianId: null,
					guardianName: null,
					applicationManagerId: null,
					applicationManagerName: null,
					technicalDepartment: null,
					securitySector: null,
					projectLeader: null,
					status: 0,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: null,
				},
				// 表单校验
				rules: {
					constructionSite: {
						rules: [{
							required: true,
							errorMessage: "施工部位不能为空",

						}]
					},
					workTeam: {
						rules: [{
							required: true,
							errorMessage: "作业班组不能为空",

						}]
					},
					liftingEquipment: {
						rules: [{
							required: true,
							errorMessage: "起重设备不能为空",

						}]
					},
					equipmentCode: {
						rules: [{
							required: true,
							errorMessage: "设备编号不能为空",

						}]
					},
					reasonForApplication: {
						rules: [{
							required: true,
							errorMessage: "申请事由不能为空",

						}]
					},
					hotWorkType: {
						rules: [{
							required: true,
							errorMessage: "动火类型不能为空",
							trigger: "change"
						}]
					},
					level: {
						rules: [{
							required: true,
							errorMessage: "级别不能为空",

						}]
					},
					startTime: {
						rules: [{
							required: true,
							errorMessage: "开始时间不能为空",

						}]
					},
					endTime: {
						rules: [{
							required: true,
							errorMessage: "结束时间不能为空",

						}]
					},
					operatives: {
						rules: [{
							required: true,
							errorMessage: "吊装人员不能为空",

						}]
					},
					depth: {
						rules: [{
							required: true,
							errorMessage: "深度不能为空",

						}]
					},
					supportMethod: {
						rules: [{
							required: true,
							errorMessage: "支护方式不能为空",

						}]
					},
					peopleCount: {
						rules: [{
							required: true,
							errorMessage: "作业人数不能为空",

						}]
					},
					homeworkCertificate: {
						rules: [{
							required: true,
							errorMessage: "作业证件不能为空",

						}]
					},
					commandId: {
						rules: [{
							required: true,
							errorMessage: "指挥人不能为空",

						}]
					},
					guardianId: {
						rules: [{
							required: true,
							errorMessage: "监护人不能为空",

						}]
					},
					applicationManagerId: {
						rules: [{
							required: true,
							errorMessage: "申请班组负责人不能为空",

						}]
					},
				},
				dataTree: [{
						text: "木工班组",
						value: "木工班组",
					},
					{
						text: "架子工班组",
						value: "架子工班组",
					},

				],
				levelList: [{
					text: "一级",
					value: "1",
				}, {
					text: "二级",
					value: "2",
				}, {
					text: "三级",
					value: "3",
				}, ],
				userList: [],
				hotWorkList: [{
					text: "电焊",
					value: "1",
				}, {
					text: "气焊",
					value: "2",
				}, {
					text: "气割",
					value: "3",
				}, {
					text: "热熔",
					value: "4",
				}, {
					text: "其他",
					value: "5",
				}, ],
				imageValue: [],
				
			}
		},
		mounted() {
	

			this.getData()
		},
		methods: {
			change(e) {
				this.show = e.show
			},
			setFormType(value) {
				this.formData.type = value
				this.$refs.popup.close()
			},
			getData() {
				listUser().then((res) => {
					this.userList = res.data.map((item) => {
						return {
							text: item.nickName,
							value: item.userId,
							deptId: item.deptId
						}
					})
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					this.formData.homeworkCertificate = this.formData.homeworkCertificate.join(',')
					addHomework(this.formData).then((response) => {
						this.$modal.msgSuccess("新增成功");
						this.$tab.reLaunch('/pages/index')
					});
				}).catch(err => {
					console.log('err', err);
				})
			},
			getSelectUserName(value, key) {

				this.formData[key] = value.detail.value[0].text
				console.log(this.formData)
			},
			//图片上传
			select(e) {
				console.log(e);
				const tempFilePaths = e.tempFilePaths;
				let that = this

				tempFilePaths.forEach(item => {
					this.imageValue.push({
						url: item.path,
						name: ""
					})
					
					uni.uploadFile({
						url: config.baseUrl + "/common/upload",
						// 上传的文件
						filePath: item,
						name: 'file',
					
						// 请求头，设置token
						header: {
							'Authorization': getToken()
						},
						// 上传成功的回调函数
						success: (uploadFileRes) => {
							let res = JSON.parse(uploadFileRes.data)
							if (res.code == '200') {
								that.formData.homeworkCertificate.push(res.fileName)
								that.imageValue.pop()
								that.imageValue.push({
									url: config.baseUrl + res.fileName,
									name: res.fileName
								})
								console.log(that.imageValue);
							} else {
								that.imageValue.pop()
								uni.showToast({
									title: "上传失败",
									icon: "none"
								})
							}

						},
						fail: error => {
						
						}
					});
				})

			},
			//图片删除
			delIMG(e) {


				this.imageValue.splice(e.index, 1);
				this.formData.homeworkCertificate.splice(e.index, 1);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #fff;
		padding: 15px;
		width: 300px;

		.warp {

			.button {
				background-color: #248aff;
				margin-bottom: 10px;
			}
		}


	}

	.content-form {
		padding: 15px;
		background-color: #fff;
	}

	::v-deep .uni-forms-item {
		margin-bottom: 17px;
	}

	::v-deep .uni-forms-item__label {
		width: 140px !important;
	}
</style>